package net.lab1024.smartadmin.module.business.environment.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.environment.domain.dto.EnvironmentAddDTO;
import net.lab1024.smartadmin.module.business.environment.domain.dto.EnvironmentQueryDTO;
import net.lab1024.smartadmin.module.business.environment.domain.dto.EnvironmentUpdateDTO;
import net.lab1024.smartadmin.module.business.environment.domain.entity.EnvironmentEntity;
import net.lab1024.smartadmin.module.business.environment.domain.vo.EnvironmentExcelVO;
import net.lab1024.smartadmin.module.business.environment.domain.vo.EnvironmentVO;
import net.lab1024.smartadmin.module.business.environment.service.EnvironmentService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ ���� ]
 *
 * @author wuhq
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:22:48
 * @since JDK1.8
 */
@RestController
@Api(tags = {"����"})
public class EnvironmentController extends BaseController {

    @Autowired
    private EnvironmentService environmentService;

    @ApiOperation(value = "����id��ȡ����",notes = "@author 何杺")
    @PostMapping("/environment/getById/{id}")
    public ResponseDTO<EnvironmentEntity> queryById(@PathVariable Long id) {

        return ResponseDTO.succData(environmentService.getById(id));
    }

    @ApiOperation(value = "��ҳ��ѯ����",notes = "@author wuhq")
    @PostMapping("/environment/page/query")
    public ResponseDTO<PageResultDTO<EnvironmentVO>> queryByPage(@RequestBody EnvironmentQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return environmentService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "��ӻ���",notes = "@author wuhq")
    @PostMapping("/environment/add")
    public ResponseDTO<String> add(@RequestBody @Validated EnvironmentAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        if(environmentService.select(addTO.getSn(),null)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return environmentService.add(addTO);
    }

    @ApiOperation(value="�޸Ļ���",notes = "@author wuhq")
    @PostMapping("/environment/update")
    public ResponseDTO<String> update(@RequestBody @Validated EnvironmentUpdateDTO updateDTO){
        if(environmentService.select(updateDTO.getSn(),updateDTO.getId())){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return environmentService.update(updateDTO);
    }

    @ApiOperation(value="����ɾ������",notes = "@author wuhq")
    @PostMapping("/environment/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return environmentService.deleteByIds(idList);
    }

    @ApiOperation(value = "��������", notes = "@author wuhq")
    @PostMapping("/environment/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //��ѯ����
        List<EnvironmentExcelVO> environmentList = environmentService.queryBatchExportData(idList);
        //��������
        ExportParams ex = new ExportParams("����", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, EnvironmentExcelVO.class, environmentList);
        downloadExcel("����", workbook, response);
    }

    @ApiOperation(value = "����ȫ��", notes = "@author wuhq")
    @PostMapping("/environment/export/all")
    public void exportAll(@RequestBody @Validated EnvironmentQueryDTO queryDTO, HttpServletResponse response) {
        //��ѯ����
        List<EnvironmentExcelVO> environmentList = environmentService.queryAllExportData(queryDTO);
        //��������
        ExportParams ex = new ExportParams("����", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, EnvironmentExcelVO.class, environmentList);
        downloadExcel("����", workbook, response);
    }

}
